package nl.ajonker.railroad.ui.drawcommand;

import nl.ajonker.railroad.pl.RailView;

import java.awt.*;
import java.awt.geom.AffineTransform;

import static nl.ajonker.railroad.dl.SwitchRail.SwitchState.STRAIGHT;

public class DrawDoubleSwitchCommand extends DrawSwitchCommand {

    @Override
    public void draw(Graphics2D g2d, RailView railView, int dimension) {
        AffineTransform saveTransform = g2d.getTransform();

        AffineTransform translate = new AffineTransform();
        translate(translate, railView);
        g2d.transform(translate);

        drawName(g2d, railView, dimension);

        AffineTransform rotate = new AffineTransform();
        rotate(rotate, railView);
        g2d.transform(rotate);

        g2d.setStroke(DrawTrackCommand.stroke);

        if (railView.getSwitchState() == STRAIGHT) {
            g2d.setColor(straightColor);
            drawQ1(g2d, dimension);
            drawQ2(g2d, dimension);
            g2d.setColor(divergingColor);
            drawQ3(g2d, dimension);
            drawQ4(g2d, dimension);
        } else {
            g2d.setColor(straightColor);
            drawQ2(g2d, dimension);
            drawQ4(g2d, dimension);
            g2d.setColor(divergingColor);
            drawQ1(g2d, dimension);
            drawQ3(g2d, dimension);

        }

        g2d.setTransform(saveTransform);
    }

    @Override
    public Shape getRegion(RailView railView, int dimension) {
        Rectangle rectangle = new Rectangle(-dimension, -dimension, 2 * dimension, 2 * dimension);
        AffineTransform at = new AffineTransform();
        at = translate(at, railView);
        at = rotate(at, railView);
//        if (railView.getType() == RailView.SwitchType.LEFT)
//            at.scale(1.0, -1.0);

        return at.createTransformedShape(rectangle);
    }

    private static void drawQ1(Graphics2D g2d, int dimension) {
        g2d.drawArc(-dimension * 4, -dimension * 2, dimension * 4, dimension * 4, 330, 30);
    }

    private static void drawQ3(Graphics2D g2d, int dimension) {
        g2d.drawArc(-dimension * 4, -dimension * 2, dimension * 4, dimension * 4, 360, 30);
    }

    private static void drawQ2(Graphics2D g2d, int dimension) {
        g2d.drawArc(0, -2 * dimension, dimension * 4, dimension * 4, 150, 30);
    }

    private static void drawQ4(Graphics2D g2d, int dimension) {
        g2d.drawArc(0, -2 * dimension, dimension * 4, dimension * 4, 180, 30);
    }
}
